package controller

import (
    "github.com/kataras/iris/v12/context"
    "github.com/kataras/iris/v12/core/router"
    "iris-server/commons/tools"
    "iris-server/src/model/vo/example"
    "iris-server/src/service"
)

type ExampleController struct {
    *service.ExampleService
}

func NewExampleController() *ExampleController {
    return &ExampleController{
        service.NewExampleService(),
    }
}

// -------------------------以上初始化时机加载--------------------------

func (c *ExampleController) ExampleRouterList(app router.Party) {
    app.Get("/ExampleList", func(ctx context.Context) {
        ctx.JSON(tools.RouterCore(ctx, &example.ExampleReq{}, c.QueryExampleList))
    })
    app.Post("/Example", func(ctx context.Context) {
        ctx.JSON(tools.RouterCore(ctx, &example.ExampleReq{}, c.addExample))
    })
    app.Put("/ExampleById", func(ctx context.Context) {
        ctx.JSON(tools.RouterCore(ctx, &example.ExampleReq{}, c.modExample))
    })
    app.Delete("/ExampleById", func(ctx context.Context) {
        ctx.JSON(tools.RouterCore(ctx, &example.ExampleReq{}, c.delExampleById))
    })
}

// -------------------------以上初始化当前Controller路由表--------------------------

// QueryExampleList 查询Example所有数据
func (c *ExampleController) QueryExampleList(req *example.ExampleReq) []example.ExampleRes {
    return c.ExampleService.QueryExampleList(req)
}

// 创建Example
func (c *ExampleController) addExample(req *example.ExampleReq) int64 {
    return c.ExampleService.AddExample(req)
}

func (c *ExampleController) modExample(req *example.ExampleReq) int64 {
    return c.ExampleService.ModExampleById(req)
}

func (c *ExampleController) delExampleById(req *example.ExampleReq) int64 {
    return c.ExampleService.DelExampleById(req)
}
